function [U B V curtail] = curtail_rank(X)
tol = 1e-8;
b = diag(X.B); % X.B should be diagonal!

curtail = 0;
if min(b) < tol,
    idx = (b > tol);
    idx = find(idx);
    U = X.U(:, idx);
    V = X.V(:, idx);
    B = X.B(:, idx);
    B = B(idx, :);
    fprintf('Rank CURTAILED!\n')
    fprintf('From rank %i  to  %i\n', size(X.B, 2), size(B, 2));
    curtail = 1;
else
    U = X.U;
    V = X.V;
    B = X.B;
end


end






